<?php

/**
 * Created by PhpStorm.
 * User: 85210755@qq.com
 * NickName: 柏宇娜
 * Date: 2019/5/31 11:59
 */

namespace lib;

use think\Exception;
use think\facade\Db;
use think\Log;
use QL\QueryList;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;

class Task
{
    protected $filter;
    protected $client;
    protected $redis;
    protected $redis_conf;
    protected $book_id;
    protected $book_name;
    protected $log;
    protected $headers = [
        'headers' => [
            'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
        ]
    ];
    private $ql_option = [
        'max'             => 5,
        'timeout'         => 10,
        'connect_timeout' => 5,
        'delay'           => 500,
        'strict'          => false,
        'verify'          => false,
        'referer'         => true,
        'protocols'       => ['https'],
        'track_redirects' => false,
        'headers'         => [
            'Connection'                => 'keep-alive',
            'Cache-Control'             => 'max-age=0',
            'Upgrade-Insecure-Requests' => '1',
            'Referer'                   => 'https://www.baidu.com/',
            'Accept'                    => 'application/json, text/plain, */*',
            'Accept-Encoding'           => 'gzip, deflate, sdch, br',
            'Cookie'                    => 'GUID=a3e1bf9a-2104-7f28-5975-36fdc3d3bce5; UM_distinctid=16a59b292e84da-0eaa4b47faa91c-3e7e045d-100200-16a59b292e9778; Hm_lvt_b33ccadfca0f88b71e592ec2bf9d3497=1559367681; Hm_lpvt_b33ccadfca0f88b71e592ec2bf9d3497=1559367681; CNZZDATA1257018823=1312955637-1559364831-https%253A%252F%252Fwww.17k.com%252F%7C1559364831; CNZZDATA5647345=cnzz_eid%3D3893204-1554477150-https%253A%252F%252Fwww.baidu.com%252F%26ntime%3D1559379383; c_channel=0; c_csc=web; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2271945827%22%2C%22%24device_id%22%3A%2216a59b291843aa-011f266dd88914-3e7e045d-1049088-16a59b29185568%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fgraph.qq.com%2Foauth2.0%2Fshow%3Fwhich%3DLogin%26display%3Dpc%26response_type%3Dcode%26client_id%3D215314%26scope%3Dget_user_info%26redirect_uri%3Dhttps%253A%252F%252Fpassport.17k.com%252Fsns%252FqqCallback.action%253FfromUrl%253Dhttp%22%2C%22%24latest_referrer_host%22%3A%22graph.qq.com%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%2C%22first_id%22%3A%22a3e1bf9a-2104-7f28-5975-36fdc3d3bce5%22%7D; Hm_lvt_9793f42b498361373512340937deb2a0=1559229077,1559382123; accessToken=id%3D71945827%26nickname%3D%25E6%259F%258F%25E5%25AE%2587%25E5%25A8%259C%26avatarUrl%3Dhttps%253A%252F%252Fcdn.static.17k.com%252Fuser%252Favatar%252F07%252F27%252F58%252F71945827.jpg%26e%3D1574935769%26s%3D41a4d8aab05f560e; Hm_lpvt_9793f42b498361373512340937deb2a0=1559367681',
            'User-Agent'                => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
        ],
    ];

    public function __construct($redis_conf = [], $filter = [])
    {
        $conf      = [
            'host' => '127.0.0.1',
            'port' => '6379',
            'auth' => ''
        ];
        $this->log = new Log;
        $this->log->init([
            'type'  => 'file',
            'debug' => FALSE,
            'path'  => './logs/'
        ]);
        $this->filter = $filter;

        $conf             = array_merge($conf, $redis_conf);
        $this->redis_conf = $conf;
        $this->redis      = new \Redis();
        $this->redis->connect($conf['host'], $conf['port']);
        if ($conf['auth']) {
            $this->redis->auth($conf['auth']);
        }
        if ($this->redis->ping() != '+PONG') {
            $this->log->error('redis connect error...');
            exit();
        }
        if (isset($conf['dbindex'])) {
            $this->redis->select($conf['dbindex']);
        }
        $this->client = new Client(['verify' => false]);
        $this->log->info('开始: ' . date('Y-m-d H:i:s'));
    }

    public function book($base, $book_id)
    {
        try {
            $ql    = QueryList::get($base . '/book/' . $book_id . '.html');
            $title = $ql->find('.Info h1 a')->text();
            if (Db::name('novel')->where('title', $title)->value('nid')) {
                echo $book_id . '== 已存在：' . $title . PHP_EOL;
                return false;
            }
            $book                    = [];
            $book['cover']           = $this->getImg($ql->find('#bookCover .book')->src);
            $book['author']          = $ql->find('.AuthorInfo .name')->text();
            $book['title']           = $title;
            $book['description']     = $ql->find('#bookInfo dd .intro a')->text();
            $book['keywords']        = $ql->find('meta[name=keywords]')->content;
            $book['total_words']     = $ql->find('.BookData em.red')->text();
            $book['click_count']     = $ql->find('#howmuchreadBook')->text();
            $book['collect_count']   = $ql->find('#flower_month')->text();
            $book['recommend_count'] = $ql->find('#flower_week')->text();
            $book['tag']             = '精品';//$ql->find('#bookInfo tr.label span')->text();
            $book['last_update']     = strtotime(substr($ql->find('#bookInfo .tit em')->text(), -19));
            $book['status']          = $ql->find('.Info > .label span')->attr('class') == 'green' ? 0 : 1;
            $book['is_show']         = 0;
            $book['origin_id']       = $book_id;

            foreach ($this->filter as $index => $item) {
                $book['keywords'] = str_replace($item, '', $book['keywords']);
            }
            $id = Db::name('novel')->insertGetId($book);
            if (!$id) {
                echo '写入数据库失败' . PHP_EOL;
                exit();
            }
            $this->log->info('采集封面完成 :' . $id);
            return $id;
        } catch (\Exception $e) {
            $this->log->error('采集封面失败:' . $e->getMessage());
            exit();
        }
    }

    public function content($base, $bid, $id, $start, $end)
    {
        $this->book_id   = $id;
        $this->book_name = Db::name('novel')->where('nid', $this->book_id)->value('title');
        $ql              = QueryList::get($base . '/list/' . $bid . '.html');
        $content         = $ql->find('.Volume')->find('dd > a')->map(function ($a) use ($base) {
            if (strpos($a->children('span')->class, 'vip')) {
                if (strpos($a->children('span')->text(), '【锁】') === false) {
                    return 'vip@' . $a->href;
                } else {
                    $this->log->info('章节锁定: ' . $a->href);
                    return 'lock';
                }
            } else {
                return $base . $a->href;
            }
        });
        //$content = $ql->find('.Volume')->find('dd > a')->attrs('href');

        //$start = $end = 0;
        $count = count($content);
        $this->log->info('章节共计：' . count($content));
        //echo '章节共计：' . $count . ',去除开头几章?' . PHP_EOL;
        //$input = (int)trim(fgets(STDIN));
        //if (is_numeric($input)) {
        //    $start = (int)$input;
        //}
        //echo '去除结尾几章?' . PHP_EOL;
        //$input = (int)trim(fgets(STDIN));
        //if (is_numeric($input)) {
        //    $end = (int)$input;
        //}
        //echo '采集共计：' . ($count - $start - $end) . '章,立即执行? (y)' . PHP_EOL;
        //$cmd = trim(fgets(STDIN));
        //if (strtolower($cmd) != 'y') {
        //    $this->log->info('小说封面已经保存,任务中断.');
        //    echo '小说封面已经保存,任务中断.' . PHP_EOL;
        //    exit();
        //}
        for ($i = $start; $i < $count - $end; $i++) {
            if ($content[$i] !== 'lock') {
                $this->redis->lPush($id, $content[$i]);
            }
        }

        $this->workerStart($bid);
        //new Server($this->redis_conf, $id, $this->log);
    }

    public function getImg($imgUrl)
    {
        header("User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        stream_context_set_default([
            'ssl' => [
                'verify_peer'      => false,
                'verify_peer_name' => false,
            ],
        ]);
        if (strpos($imgUrl, "http") !== 0) {
            $imgUrl = 'http:' . $imgUrl;
        }
        //获取请求头，遇到https路径，需要自己配置好本机PHP开启SSL并设置ca证书
        $heads = get_headers($imgUrl, 1);
        //死链检测
        if (strpos($heads[0], "200") === false && strpos($heads[0], "OK") === false) {
            $this->log->error("get_headers error");
            exit();
        }

        $content_type = strtolower(stristr($heads['Content-Type'], "image"));
        $sufix        = ['image/jpeg' => '.jpg', 'image/png' => '.png', 'image/svg+xml' => '.svg', 'image/gif' => '.gif'];
        //格式验证(扩展名验证和Content-Type验证)
        $fileType = isset($sufix[$content_type]) ? $sufix[$content_type] : null;

        if (!$fileType) {
            $this->log->error("Content-Type error");
            exit();
        }

        ob_start();
        $context = stream_context_create(['timeout' => 10, 'http' => ['follow_location' => false, 'method' => 'GET', 'header' => 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36\r\n']]); // don't follow redirects

        //请确保php.ini中的fopen wrappers已经激活，即allow_url_fopen = On
        readfile($imgUrl, false, $context);
        $img = ob_get_clean();
        //$uriSize = strlen($img); //得到图片大小
        $path     = '/upload/novel/' . date('Ymd') . '/';
        $savePath = '/home/alex/www/funny/public' . $path;
        //创建保存位置
        if (!file_exists($savePath)) {
            mkdir($savePath, 0775, true);
        }

        $tmpName = time() . rand(10000, 99999) . $fileType;

        $flag = file_put_contents($savePath . $tmpName, $img);
        if ($flag === false) {
            $this->log->error('下载封面失败');
            exit();
        }
        return $path . $tmpName;
    }

    public function getCategory($base_url, $ticket)
    {
        $option = [
            'headers' => [
                'Origin'     => 'https://m.yymh666.com/',
                'Referer'    => 'https://m.yymh666.com/',
                'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
                'ticket'     => $ticket
            ]
        ];

        $client = new Client($option);
        for ($i = 14; $i < 17; $i++) {
            $response = $client->post($base_url . '?type=cartoon&paged=true&size=20&page=' . $i . '&category=&a=index&f=books');
            $res      = json_decode($response->getBody()->getContents(), true);
            if ($res['code'] == 200) {
                foreach ($res['content']['list'] as $index => $re) {
                    echo $i . ':' . $index . PHP_EOL;
                    $this->redis->lPush('all', $re['name']);
                    $cover = $this->getComicCover($re['coverUrl']) . PHP_EOL;
                    $row   = [
                        'name'          => $re['name'],
                        'desc'          => $re['description'],
                        'author'        => $re['author'],
                        'cover'         => $cover,
                        'theme_tag'     => $re['tags'],
                        'flock_tag'     => $re['category'],
                        'status'        => $re['status']['name'] == 'serialized' ? 1 : 2,
                        'territory'     => '日本',
                        'createtime'    => strtotime($re['crtime']),
                        'lastupdate'    => strtotime($re['uptime']),
                        'lastchapterid' => $re['lastChapter'],
                        'lastchapter'   => 0,
                        'click'         => $re['readCount'],
                        'collect'       => 0,
                        'comment'       => 0,
                        'is_show'       => 1,
                        'is_recommend'  => $re['recommend'],
                        'isvip'         => 1,
                        'ids'           => $re['id'],
                    ];
                    $id    = Db::name('comic')->insertGetId($row);
                    if (!$id) {
                        echo $i . ':' . $index;
                        exit();
                    }
                }
            } else {
                echo $i . '获取接口失败!~' . PHP_EOL;
                exit;
            }
        }

    }

    public function getComicCover($imgUrl, $path = '')
    {
        if (!$path) {
            $path     = '/upload/comic/' . date('Ymd') . '/';
            $savePath = '/home/alex/www/funny/public' . $path;
        } else {
            $savePath = '/www/funny/files' . $path;
        }

        //创建保存位置
        if (!file_exists($savePath)) {
            mkdir($savePath, 0775, true);
        }
        $tmpName = time() . rand(10000, 99999) . '.' . pathinfo($imgUrl)['extension'];

        try {
            $proxy = $this->randProxy();
            $img   = $this->client->get($imgUrl, [
                'save_to' => $savePath . $tmpName,
                'timeout' => 10,
                'proxy'   => $proxy,
                'headers' => [
                    'Referer'    => 'https://m.yymh666.com/',
                    'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
                ]
            ]);
        } catch (\Exception $e) {
            $this->log->info($proxy['https'] . '====' . $e->getMessage());
            echo '失败:' . $imgUrl . PHP_EOL;
            return $imgUrl;
        }

        if ($img->getStatusCode() != 200) {
            $this->log->error('下载封面失败');
            exit();
        }

        return $path . $tmpName;
        //return $savePath . $tmpName;
    }

    public function getComicChapter($base_url, $ticket)
    {
        $option = [
            'headers' => [
                'Origin'     => $base_url,
                'Referer'    => $base_url,
                'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
                'ticket'     => $ticket
            ]
        ];
        $list   = Db::name('comic')->where('id', '>', 238)->column('ids', 'id');
        $client = new Client($option);
        foreach ($list as $id => $ids) {
            $url      = 'https://m.yymh666.com/?bookId=' . $ids . '&a=index&f=directory';
            $response = $client->post($url);
            $res      = json_decode($response->getBody()->getContents(), true);
            if ($res['code'] == 200) {
                foreach ($res['content'] as $index => $re) {
                    //if ($id == 46 && $index == 0) continue;//FIXME need delete
                    $path = '/comic/' . $id . '/';
                    if ($re['coverUrl'] == 'https://img.cb133.com.jpg' || $re['coverUrl'] == 'https://img.cb133.com/.jpg') {
                        $cover = 'https://img.cb133.com.jpg';
                    } else {
                        $cover = $this->getComicCover($re['coverUrl'], $path);
                    }
                    $row        = [
                        'name'          => $re['title'] ?? '番外章节',
                        'comic_id'      => $id,
                        'chapter_cover' => $cover,
                        'create_time'   => strtotime($re['crtime']),
                        'sort'          => $index,
                        'type'          => 1,//0免费1限免2收费
                        'gold'          => $re['points'] ?? 0,
                        'vipgold'       => $re['points'] ?? 0,
                        'is_show'       => 1,
                        'ids'           => $re['id'],
                        'temp'          => $re['content'],
                    ];
                    $chapter_id = Db::name('comic_chapter')->insertGetId($row);
                    if (!$chapter_id) {
                        echo '章节插入失败==' . $id . ':' . $index . PHP_EOL;
                        exit();
                    }
                    echo $id . ':' . $index . PHP_EOL;
                }
            } else {
                echo $id . '章节获取接口失败!~' . PHP_EOL;
                exit;
            }
        }
    }

    public function fixComicChaptrer($id)
    {
        $list = Db::name('comic_chapter')->where('comic_id', $id)->order('sort')->column('comic_id,chapter_cover,temp,sort', 'chapter_id');
        if (Db::name('comic_chapter_images')->where('comic_id', $id)->find()) {
            echo '已经采过.' . PHP_EOL;
            exit();
        }
        $dom = new \DOMDocument();

        foreach ($list as $cid => $item) {
            $now = time();
            $dom->loadHTML($item['temp']);
            $imgs = $dom->getElementsByTagName('img');
            $path = '/comic/' . $item['comic_id'] . '/' . $item['sort'] . '/';
            $sort = $imgs->length;

            foreach ($imgs as $img) {
                $image = $this->getComicCover($img->getAttribute('src'), $path);
                $row   = [
                    'comic_id'   => $item['comic_id'],
                    'chapter_id' => $cid,
                    'sort'       => $sort--,
                    'createtime' => $now,
                    'img'        => $image
                ];
                $id    = Db::name('comic_chapter_images')->insertGetId($row);
                if (!$id) {
                    $str = '错误：' . PHP_EOL . $image . $img->getAttribute('src') . PHP_EOL;
                    $this->log->error($str);
                    echo $str;
                    exit();
                }
                echo $item['comic_id'] . '-' . $item['sort'] . '-' . $sort . PHP_EOL;
            }

        }
    }

    public function randProxy()
    {
        if ($this->redis->sCard('proxy') < 3) {
            $baseuri = 'http://www.89ip.cn/index_';
            //$baseuri = 'https://www.kuaidaili.com/free/intr/';
            for ($j = 1; $j < 10; $j++) {
                $proxy = QueryList::get($baseuri . $j)->find('.layui-table > tbody > tr')->map(function ($tr) {
                    return $tr->find('td')->texts();
                })->all();

                for ($i = 1; $i <= 15; $i++) {
                    if (stream_socket_client(trim($proxy[$i][0]) . ":" . trim($proxy[$i][1]), $errno, $errstr, 1)) {
                        $this->redis->sadd('proxy', trim($proxy[$i][0]) . ':' . trim($proxy[$i][1]));
                    }
                }
            }
        } else {
            //foreach ($this->redis->smembers('proxy') as $v) {
            //    if (!stream_socket_client($v, $errno, $errstr, 3)) {
            //        $this->redis->srem('proxy', $v);
            //    }
            //}
        }

        $proxy = $this->redis->srandmember('proxy');
        if ($proxy) return ['https' => 'tcp://' . $proxy];
        $this->log->error('没有可用代理.');
        return false;
    }

    public function aloneImg()
    {
        $list = Db::name('comic_chapter_images')->whereRaw('img REGEXP "^http"')->column('img,comic_id,sort', 'id');
        $len  = count($list);
        foreach ($list as $id => $item) {
            echo $len-- . PHP_EOL;
            if ($item['img'] == 'https://img.cb133.com.jpg' || $item['img'] == 'https://img.cb133.com/.jpg') {
                continue;
            }
            $path = '/comic/' . $item['comic_id'] . '/' . $item['sort'] . '/';
            $img  = $this->getComicCover($item['img'], $path);
            Db::name('comic_chapter_images')->where('id', $id)->update(['img' => $img]);
        }
    }

    public function workerStart($bid)
    {
        if (!$this->redis) {
            $this->redis = new \Redis();
            $this->redis->connect($this->redis_conf['host'], $this->redis_conf['port']);
            if ($this->redis_conf['auth']) {
                $this->redis->auth($this->redis_conf['auth']);
            }
            if ($this->redis_conf['dbindex']) {
                $this->redis->select($this->redis_conf['dbindex']);
            }
        }

        while ($url = $this->redis->rpoplpush($this->book_id, 'finishi' . $this->book_id)) {
            try {
                $this->pickup($url, $this->redis->llen('finishi' . $this->book_id));
            } catch (\Exception $e) {
                $this->redis->lPush("errorlist" . $this->book_id, $url);
                $this->log->error("采集失败:" . $url . PHP_EOL . $e->getMessage());
            }
        }

        sleep(3);
        if ($this->redis->lLen("errorlist" . $this->book_id) > 0) {
            while ($url = $this->redis->rpoplpush($this->book_id, 'errorlist' . $this->book_id)) {
                try {
                    $this->pickup($url, $this->redis->llen('finishi' . $this->book_id));
                } catch (\Exception $e) {
                    $this->redis->lPush("errorlist" . $this->book_id, $url);
                    $this->log->error("采集失败:" . $url . PHP_EOL . $e->getMessage());
                }
            }

            if ($this->redis->lLen("errorlist" . $this->book_id) > 3) {
                $this->redis->close();
                $this->redis = null;
                $this->log->info('ID：' . $this->book_id . ' 任务失败退出.');
                echo 'ID：' . $this->book_id . ' 任务失败退出.' . PHP_EOL;
                exit();
            } else {
                $this->redis->del("errorlist" . $this->book_id);
            }
        }

        if (!$this->redis->get('lock')) {
            $this->redis->set('lock', true);
            $this->afterTask();

            $this->redis->del('finishi' . $this->book_id);
            $this->redis->del('lock');
            $this->log->info('the last：' . $this->book_id . '正常结束: ' . date('Y-m-d H:i:s'));
            echo $bid . '===' . $this->book_name . PHP_EOL;
        }
    }

    public function pickup($url, $no)
    {
        if (strpos($url, 'vip@') !== false) {
            $url = str_replace('vip@', '', $url);
            $url = str_replace('/chapter/', '', $url);
            $url = str_replace('.html', '', $url);
            list($book_id, $chapter_id) = explode('/', $url);
            $url = 'https://www.17k.com/ck/book/' . $book_id . '/' . 'chapter/' . $chapter_id . '?subAllPrice=1&appKey=2406394919';
            $c1  = [
                'CNZZDATA5647345'           => 'cnzz_eid%3D740720203-1559260832-%26ntime%3D1566866992',
                'UM_distinctid'             => '16b0b5abfbcc2-065160f7b651e18-14397640-1fa400-16b0b5abfbd25d',
                'accessToken'               => 'id=71945827&nickname=%E6%9F%8F%E5%AE%87%E5%A8%9C&avatarUrl=https%3A%2F%2Fcdn.static.17k.com%2Fuser%2Favatar%2F07%2F27%2F58%2F71945827.jpg&e=1575102525&s=2935dfc1ebbe68a8',
                'c_csc'                     => 'web',
                'sensorsdata2015jssdkcross' => '%7B%22distinct_id%22%3A%2271945827%22%2C%22%24device_id%22%3A%2216b0b5abf6b2f8-0fccbb7d58f8d78-14397640-2073600-16b0b5abf6c417%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%229e672dd6-4601-8ef2-d1ad-414d6f7e80f8%22%7D',
            ];

            //$c1       = [
            //    'CNZZDATA5647345'           => 'cnzz_eid%3D740720203-1559260832-%26ntime%3D1566779481',
            //    'UM_distinctid'             => '16b0b5abfbcc2-065160f7b651e18-14397640-1fa400-16b0b5abfbd25d',
            //    'accessToken'               => 'id%3D71945827%26nickname%3D%25E6%259F%258F%25E5%25AE%2587%25E5%25A8%259C%26avatarUrl%3Dhttps%253A%252F%252Fcdn.static.17k.com%252Fuser%252Favatar%252F07%252F27%252F58%252F71945827.jpg%26e%3D1575102525%26s%3D2935dfc1ebbe68a8',
            //    'c_csc'                     => 'web',
            //    'sensorsdata2015jssdkcross' => '%7B%22distinct_id%22%3A%2271945827%22%2C%22%24device_id%22%3A%2216b0b5abf6b2f8-0fccbb7d58f8d78-14397640-2073600-16b0b5abf6c417%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%229e672dd6-4601-8ef2-d1ad-414d6f7e80f8%22%7D'
            //];
            $jar      = CookieJar::fromArray($c1, 'www.17k.com');
            $client   = new Client(['cookies' => $jar]);
            $response = $client->request('GET', $url, ['verify' => '/home/alex/www/pickup/_.17k.com.pem']);

            $arr = json_decode($response->getBody()->getContents(), true);

            $section_info = explode(' ', $arr['data']['name']);
            $chinese_no   = $section_info[0];
            $tit          = $section_info[1];
            $time         = substr($arr['data']['publishDate'], 0, 10);
            $content      = $arr['data']['content'][0]['text'];
        } else {
            //随机UA
            //$this->ql_option['User-Agent'] = $this->randUserAgent('pc');
            //启用代理
            //$proxy                         = $this->randProxy();
            //if ($proxy) {
            //    $this->ql_option['proxy'] = $proxy;
            //}
            $ql      = QueryList::get($url, $this->ql_option);
            $content = $ql->find('#readArea > .content > .p')
                ->removeClass('copy')->removeClass('qrcode')->removeClass('chapter_text_ad')->remove('#banner_content')
                ->text();
            if (!$content) {
                $this->log->error('获取內容失败' . $url);
                return;
            }
            //$ql->find('.area > infoPath > a')->eq(2)->text();
            $content      = str_replace('本书首发来自17K小说网，第一时间看正版内容！', '', $content);
            $content      = $content = str_replace('下载17K客户端，《' . $this->book_name . '》最新章节无广告纯净阅读。', '', $content);
            $content      = str_replace('17K客户端专享，签到即送VIP，免费读全站。', '', $content);
            $section_info = $ql->find('#readArea h1')->text();
            $chinese_no   = trim(substr($section_info, 0, stripos($section_info, ' ')));
            $tit          = trim(substr($section_info, stripos($section_info, ' ')));
            $time         = strtotime(substr(trim($ql->find('#readArea .chapter_update_time')->text()), -19));
        }

        $row = [
            'nid'                => $this->book_id,
            'section_no'         => $no,
            'section_chinese_no' => $chinese_no,
            'section_title'      => $tit,
            'add_time'           => $time
        ];
        $sid = Db::name('novel_section')->insertGetId($row);
        if (!$sid) {
            $this->log->error('pickup写入数据库失败:' . $url);
        }
        $path = '/home/alex/www/files/' . $this->book_id;
        if (!is_dir($path)) {
            mkdir($path, 0775);
        }
        $file = $path . DIRECTORY_SEPARATOR . $sid . '.txt';
        if (!file_put_contents($file, $content)) {
            $this->log->error('pickup写入内容失败:' . $url . $path);
        }
    }

    public function afterTask()
    {
        $list  = Db::name('novel_section')->where('nid', $this->book_id)->order('section_no ASC')->field('sid,section_no')->select();
        $count = count($list);
        foreach ($list as $index => $item) {
            $data = ['prev' => 0];
            if ($index > 0) {
                $data['prev'] = $list[$index - 1]['sid'];
            }
            if ($index + 1 == $count) {
                $data['next'] = 0;
            } else {
                $data['next'] = $list[$index + 1]['sid'];
            }
            Db::name('novel_section')->where('sid', $item['sid'])->update($data);
        }
        Db::name('novel')->where('nid', $this->book_id)->update(['last_section' => $item['sid'], 'last_section_title' => $item['section_title']]);
    }
}
